<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<script type="text/javascript">
			var fun=function(){
				console.log(event.key);
				console.log(event.keyCode);
				if(event.keyCode<48||event.keyCode>57)
				{
					console.error("只能输入数字");
					event.preventDefault()
				}
			}
		</script>
	</head>
	<body>
		请输入数字：<input type="text" onkeypress="fun()">
	</body>
</html>
